package com.guimin.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guimin.common.util.exception.BaseException;
import com.guimin.common.vo.R;
import com.guimin.fuwu.dao.GuiminServiceDao;
import com.guimin.fuwu.entity.GuiminService;
import com.guimin.recycle.entity.GuiminRecycle;
import com.guimin.recycle.dao.GuiminRecycleDao;
import com.guimin.recycle.service.GuiminRecycleService;
import com.guimin.recycle.vo.recycleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (GuiminRecycle)表服务实现类
 *
 * @author makejava
 * @since 2021-11-24 16:42:39
 */
@Service("guiminRecycleService")
public class GuiminRecycleServiceImpl extends ServiceImpl<GuiminRecycleDao, GuiminRecycle> implements GuiminRecycleService {
    @Resource
    private GuiminRecycleDao guiminRecycleDao;

    /**
     * 添加一级类
     * @param id
     * @param name
     * @return
     */
    public R<?> addRecycleNoe(Long id, String name) {
        try {
            if(id == null){
                throw  new BaseException("用户为登录");
            }
            if(name == null || name == ""){
                throw  new BaseException("参数名称为空");
            }
            //查询是否存在
            QueryWrapper<GuiminRecycle> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Integer integer = guiminRecycleDao.selectCount(wrapper);
            if(integer != 0){
                return R.fail("该类名称已存在");
            }
            GuiminRecycle recycle = new GuiminRecycle();
            recycle.setId(new Date().getTime());
            recycle.setName(name);
            recycle.setUserId(id);
            int insert = guiminRecycleDao.insert(recycle);
            if(insert > 0){
              return  R.success("添加成功");
            }else {
              return R.fail("添加失败");
            }
        }catch (Exception e){
            throw new BaseException("请求异常，请稍后重试");
        }
    }

    /**
     * 添加二级类
     * @param id
     * @param recycle
     * @return
     */
    public R<?> addRecycleTwo(Long id, GuiminRecycle recycle) {
        try {
            if(id == null){
                throw  new BaseException("用户为登录");
            }
            if(recycle == null){
                throw  new BaseException("参数为空");
            }
            //查询是否存在
            QueryWrapper<GuiminRecycle> wrapper = new QueryWrapper<>();
            wrapper.eq("two_name",recycle.getTwoName());

            Integer integer = guiminRecycleDao.selectCount(wrapper);
            if(integer != 0){
                return R.fail("该类名称已存在");
            }
            recycle.setUserId(id);
            recycle.setId(new Date().getTime());
            int insert = guiminRecycleDao.insert(recycle);
            if(insert > 0){
                return  R.success("添加成功");
            }else {
                return R.fail("添加失败");
            }
        }catch (Exception e){
            throw new BaseException("请求异常，请稍后重试");
        }
    }

    /**
     * 查询垃圾回收列表(一级)
     * @param id
     * @return
     */
    public R<?> getRecycleListNoe(Long id) {
        try {
            if(id == null){
                throw  new BaseException("用户为登录");
            }
            QueryWrapper<GuiminRecycle> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",id);
            wrapper.isNull("super_id");
            List<GuiminRecycle> recycles = guiminRecycleDao.selectList(wrapper);
            if(recycles.size() != 0){
                return R.success("查询成功",recycles);
            }else {
                return R.fail("查询失败");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new BaseException("请求发送异常，请稍后重试");
        }
    }

    /**
     * 查询垃圾回收列表(二级)
     * @param id
     * @return
     */
    public R<?> getRecycleListTwo(Long id) {
        try {
            if(id == null){
                throw  new BaseException("用户为登录");
            }
            QueryWrapper<GuiminRecycle> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",id);
            wrapper.isNotNull("super_id");
            List<GuiminRecycle> recycles = guiminRecycleDao.selectList(wrapper);
            ArrayList<recycleVo> recycleVos = new ArrayList<>();
            if(recycles.size() != 0){
                for (GuiminRecycle recycle : recycles){
                    recycleVo recycleVo = new recycleVo();
                    BeanUtils.copyProperties(recycle,recycleVo);
                    recycleVos.add(recycleVo);
                }
                return R.success("查询成功",recycleVos);
            }else {
                return R.fail("查询失败");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new BaseException("请求发送异常，请稍后重试");
        }
    }

    /**
     * 按id删除字段
     * @param userId
     * @param id
     * @return
     */
    public R<?> deleteRecycle(Long userId, Long id) {
        try {
            if(userId == null){
                return R.fail("用户未登录");
            }
            if(id == null){
                return R.fail("为获取id");
            }
            int i = guiminRecycleDao.deleteById(id);
            if(i > 0){
                return R.success("删除成功");
            }else {
                return R.fail("删除失败，无此id");
            }
        }catch (Exception e){
            throw new BaseException("删除异常，请稍后重试");
        }
    }

    /**
     * 编辑二级类
     * @param recycle
     * @return
     */
    public R<?> alterRecycle(GuiminRecycle recycle) {
        try {
            QueryWrapper<GuiminRecycle> wrapper = new QueryWrapper<>();
            wrapper.eq("id",recycle.getId());
            int update = guiminRecycleDao.update(recycle, wrapper);
            if(update > 0){
                return R.success("编辑成功");
            }else {
                return R.fail("编辑失败，无此id");
            }
        }catch (Exception e){
            throw new BaseException("编辑异常，请稍后重试");
        }
    }
}
